package com.gc.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 管理员界面过滤器
 */
@WebFilter(filterName = "TeacherFilter", urlPatterns = { "/main/teacher/*", "/admin/indexTeacher.jsp" })
public class TeacherFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpSession session = request.getSession();
        if(session.getAttribute("Authority") == null || !session.getAttribute("Authority").equals("3")) {
            String authority = (String)session.getAttribute("Authority");
            HttpServletResponse response =  (HttpServletResponse)servletResponse;
            //3秒后跳转
            response.setHeader("refresh", "3;URL=/login.jsp");
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("<h3>您没有权限访问！3秒后返回登录界面</h2>");
        } else {
            filterChain.doFilter(servletRequest,servletResponse);
        }


    }

    @Override
    public void destroy() {

    }


}
